Home | History | Annotate | Line # | Download | only in config
mkmakefile.c revision 1.9
      1  1.9     enami /*	$NetBSD: mkmakefile.c,v 1.9 2009/02/15 03:45:44 enami Exp $	*/
      2  1.1   thorpej 
      3  1.1   thorpej /*
      4  1.1   thorpej  * Copyright (c) 1992, 1993
      5  1.1   thorpej  *	The Regents of the University of California.  All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * This software was developed by the Computer Systems Engineering group
      8  1.1   thorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1   thorpej  * contributed to Berkeley.
     10  1.1   thorpej  *
     11  1.1   thorpej  * All advertising materials mentioning features or use of this software
     12  1.1   thorpej  * must display the following acknowledgement:
     13  1.1   thorpej  *	This product includes software developed by the University of
     14  1.1   thorpej  *	California, Lawrence Berkeley Laboratories.
     15  1.1   thorpej  *
     16  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     17  1.1   thorpej  * modification, are permitted provided that the following conditions
     18  1.1   thorpej  * are met:
     19  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     20  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     21  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     23  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     24  1.1   thorpej  * 3. Neither the name of the University nor the names of its contributors
     25  1.1   thorpej  *    may be used to endorse or promote products derived from this software
     26  1.1   thorpej  *    without specific prior written permission.
     27  1.1   thorpej  *
     28  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.1   thorpej  * SUCH DAMAGE.
     39  1.1   thorpej  *
     40  1.1   thorpej  *	from: @(#)mkmakefile.c	8.1 (Berkeley) 6/6/93
     41  1.1   thorpej  */
     42  1.1   thorpej 
     43  1.1   thorpej #if HAVE_NBTOOL_CONFIG_H
     44  1.1   thorpej #include "nbtool_config.h"
     45  1.1   thorpej #endif
     46  1.1   thorpej 
     47  1.1   thorpej #include <sys/param.h>
     48  1.1   thorpej #include <ctype.h>
     49  1.1   thorpej #include <errno.h>
     50  1.1   thorpej #include <stdio.h>
     51  1.1   thorpej #include <stdlib.h>
     52  1.1   thorpej #include <string.h>
     53  1.6  christos #include <err.h>
     54  1.1   thorpej #include "defs.h"
     55  1.1   thorpej #include "sem.h"
     56  1.1   thorpej 
     57  1.1   thorpej /*
     58  1.1   thorpej  * Make the Makefile.
     59  1.1   thorpej  */
     60  1.1   thorpej 
     61  1.1   thorpej static const char *srcpath(struct files *);
     62  1.1   thorpej 
     63  1.1   thorpej static const char *prefix_prologue(const char *);
     64  1.2       erh static const char *filetype_prologue(struct filetype *);
     65  1.2       erh 
     66  1.1   thorpej 
     67  1.5       dsl static void emitdefs(FILE *);
     68  1.5       dsl static void emitfiles(FILE *, int, int);
     69  1.1   thorpej 
     70  1.5       dsl static void emitobjs(FILE *);
     71  1.5       dsl static void emitcfiles(FILE *);
     72  1.5       dsl static void emitsfiles(FILE *);
     73  1.5       dsl static void emitrules(FILE *);
     74  1.5       dsl static void emitload(FILE *);
     75  1.5       dsl static void emitincludes(FILE *);
     76  1.5       dsl static void emitappmkoptions(FILE *);
     77  1.8      cube static void emitsubs(FILE *, const char *, const char *, int);
     78  1.1   thorpej 
     79  1.1   thorpej int
     80  1.1   thorpej mkmakefile(void)
     81  1.1   thorpej {
     82  1.1   thorpej 	FILE *ifp, *ofp;
     83  1.1   thorpej 	int lineno;
     84  1.5       dsl 	void (*fn)(FILE *);
     85  1.1   thorpej 	char *ifname;
     86  1.1   thorpej 	char line[BUFSIZ], buf[200];
     87  1.1   thorpej 
     88  1.1   thorpej 	/* Try a makefile for the port first.
     89  1.1   thorpej 	 */
     90  1.1   thorpej 	(void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
     91  1.1   thorpej 	    machine, machine);
     92  1.1   thorpej 	ifname = sourcepath(buf);
     93  1.1   thorpej 	if ((ifp = fopen(ifname, "r")) == NULL) {
     94  1.1   thorpej 		/* Try a makefile for the architecture second.
     95  1.1   thorpej 		 */
     96  1.1   thorpej 		(void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
     97  1.1   thorpej 		    machinearch, machinearch);
     98  1.4      cube 		free(ifname);
     99  1.1   thorpej 		ifname = sourcepath(buf);
    100  1.4      cube 		ifp = fopen(ifname, "r");
    101  1.1   thorpej 	}
    102  1.4      cube 	if (ifp == NULL) {
    103  1.6  christos 		warn("cannot read %s", ifname);
    104  1.4      cube 		goto bad2;
    105  1.1   thorpej 	}
    106  1.5       dsl 
    107  1.1   thorpej 	if ((ofp = fopen("Makefile.tmp", "w")) == NULL) {
    108  1.6  christos 		warn("cannot write Makefile");
    109  1.4      cube 		goto bad1;
    110  1.1   thorpej 	}
    111  1.5       dsl 
    112  1.5       dsl 	emitdefs(ofp);
    113  1.5       dsl 
    114  1.1   thorpej 	lineno = 0;
    115  1.1   thorpej 	while (fgets(line, sizeof(line), ifp) != NULL) {
    116  1.1   thorpej 		lineno++;
    117  1.8      cube 		if (version < 20090214 && line[0] != '%') {
    118  1.5       dsl 			fputs(line, ofp);
    119  1.1   thorpej 			continue;
    120  1.1   thorpej 		}
    121  1.1   thorpej 		if (strcmp(line, "%OBJS\n") == 0)
    122  1.1   thorpej 			fn = emitobjs;
    123  1.1   thorpej 		else if (strcmp(line, "%CFILES\n") == 0)
    124  1.1   thorpej 			fn = emitcfiles;
    125  1.1   thorpej 		else if (strcmp(line, "%SFILES\n") == 0)
    126  1.1   thorpej 			fn = emitsfiles;
    127  1.1   thorpej 		else if (strcmp(line, "%RULES\n") == 0)
    128  1.1   thorpej 			fn = emitrules;
    129  1.1   thorpej 		else if (strcmp(line, "%LOAD\n") == 0)
    130  1.1   thorpej 			fn = emitload;
    131  1.1   thorpej 		else if (strcmp(line, "%INCLUDES\n") == 0)
    132  1.1   thorpej 			fn = emitincludes;
    133  1.1   thorpej 		else if (strcmp(line, "%MAKEOPTIONSAPPEND\n") == 0)
    134  1.1   thorpej 			fn = emitappmkoptions;
    135  1.8      cube 		else if (strncmp(line, "%VERSION ", sizeof("%VERSION ")-1) == 0) {
    136  1.8      cube 			int newvers;
    137  1.8      cube 			if (sscanf(line, "%%VERSION %d\n", &newvers) != 1) {
    138  1.8      cube 				cfgxerror(ifname, lineno, "syntax error for "
    139  1.8      cube 				    "%%VERSION");
    140  1.8      cube 			} else
    141  1.8      cube 				setversion(newvers);
    142  1.8      cube 			continue;
    143  1.8      cube 		} else {
    144  1.8      cube 			if (version < 20090214)
    145  1.8      cube 				cfgxerror(ifname, lineno,
    146  1.8      cube 				    "unknown %% construct ignored: %s", line);
    147  1.8      cube 			else
    148  1.8      cube 				emitsubs(ofp, line, ifname, lineno);
    149  1.1   thorpej 			continue;
    150  1.1   thorpej 		}
    151  1.5       dsl 		(*fn)(ofp);
    152  1.1   thorpej 	}
    153  1.5       dsl 
    154  1.5       dsl 	fflush(ofp);
    155  1.5       dsl 	if (ferror(ofp))
    156  1.5       dsl 		goto wrerror;
    157  1.5       dsl 
    158  1.1   thorpej 	if (ferror(ifp)) {
    159  1.6  christos 		warn("error reading %s (at line %d)", ifname, lineno);
    160  1.1   thorpej 		goto bad;
    161  1.1   thorpej 	}
    162  1.5       dsl 
    163  1.1   thorpej 	if (fclose(ofp)) {
    164  1.1   thorpej 		ofp = NULL;
    165  1.1   thorpej 		goto wrerror;
    166  1.1   thorpej 	}
    167  1.1   thorpej 	(void)fclose(ifp);
    168  1.5       dsl 
    169  1.1   thorpej 	if (moveifchanged("Makefile.tmp", "Makefile") != 0) {
    170  1.6  christos 		warn("error renaming Makefile");
    171  1.4      cube 		goto bad2;
    172  1.1   thorpej 	}
    173  1.1   thorpej 	free(ifname);
    174  1.1   thorpej 	return (0);
    175  1.4      cube 
    176  1.1   thorpej  wrerror:
    177  1.6  christos 	warn("error writing Makefile");
    178  1.1   thorpej  bad:
    179  1.1   thorpej 	if (ofp != NULL)
    180  1.1   thorpej 		(void)fclose(ofp);
    181  1.4      cube  bad1:
    182  1.4      cube 	(void)fclose(ifp);
    183  1.1   thorpej 	/* (void)unlink("Makefile.tmp"); */
    184  1.4      cube  bad2:
    185  1.1   thorpej 	free(ifname);
    186  1.1   thorpej 	return (1);
    187  1.1   thorpej }
    188  1.1   thorpej 
    189  1.8      cube static void
    190  1.8      cube emitsubs(FILE *fp, const char *line, const char *file, int lineno)
    191  1.8      cube {
    192  1.9     enami 	char *nextpct;
    193  1.9     enami 	const char *optname;
    194  1.8      cube 	struct nvlist *option;
    195  1.8      cube 
    196  1.8      cube 	while (*line != '\0') {
    197  1.8      cube 		if (*line != '%') {
    198  1.8      cube 			fputc(*line++, fp);
    199  1.8      cube 			continue;
    200  1.8      cube 		}
    201  1.8      cube 
    202  1.8      cube 		line++;
    203  1.8      cube 		nextpct = strchr(line, '%');
    204  1.8      cube 		if (nextpct == NULL) {
    205  1.8      cube 			cfgxerror(file, lineno, "unbalanced %% or "
    206  1.8      cube 			    "unknown construct");
    207  1.8      cube 			return;
    208  1.8      cube 		}
    209  1.8      cube 		*nextpct = '\0';
    210  1.8      cube 
    211  1.8      cube 		if (*line == '\0')
    212  1.8      cube 			fputc('%', fp);
    213  1.8      cube 		else {
    214  1.8      cube 			optname = intern(line);
    215  1.8      cube 			if (!DEFINED_OPTION(optname)) {
    216  1.8      cube 				cfgxerror(file, lineno, "unknown option %s",
    217  1.8      cube 				    optname);
    218  1.8      cube 				return;
    219  1.8      cube 			}
    220  1.8      cube 
    221  1.8      cube 			if ((option = ht_lookup(opttab, optname)) == NULL)
    222  1.8      cube 				option = ht_lookup(fsopttab, optname);
    223  1.8      cube 			if (option != NULL)
    224  1.8      cube 				fputs(option->nv_str ? option->nv_str : "1",
    225  1.8      cube 				    fp);
    226  1.8      cube 			/* Otherwise it's not a selected option and we don't
    227  1.8      cube 			 * output anything. */
    228  1.8      cube 		}
    229  1.8      cube 
    230  1.8      cube 		line = nextpct+1;
    231  1.8      cube 	}
    232  1.8      cube }
    233  1.8      cube 
    234  1.1   thorpej /*
    235  1.1   thorpej  * Return (possibly in a static buffer) the name of the `source' for a
    236  1.1   thorpej  * file.  If we have `options source', or if the file is marked `always
    237  1.1   thorpej  * source', this is always the path from the `file' line; otherwise we
    238  1.1   thorpej  * get the .o from the obj-directory.
    239  1.1   thorpej  */
    240  1.1   thorpej static const char *
    241  1.1   thorpej srcpath(struct files *fi)
    242  1.1   thorpej {
    243  1.1   thorpej #if 1
    244  1.1   thorpej 	/* Always have source, don't support object dirs for kernel builds. */
    245  1.1   thorpej 	return (fi->fi_path);
    246  1.1   thorpej #else
    247  1.1   thorpej 	static char buf[MAXPATHLEN];
    248  1.1   thorpej 
    249  1.1   thorpej 	if (have_source || (fi->fi_flags & FI_ALWAYSSRC) != 0)
    250  1.1   thorpej 		return (fi->fi_path);
    251  1.1   thorpej 	if (objpath == NULL) {
    252  1.6  christos 		cfgerror("obj-directory not set");
    253  1.1   thorpej 		return (NULL);
    254  1.1   thorpej 	}
    255  1.1   thorpej 	(void)snprintf(buf, sizeof buf, "%s/%s.o", objpath, fi->fi_base);
    256  1.1   thorpej 	return (buf);
    257  1.1   thorpej #endif
    258  1.1   thorpej }
    259  1.1   thorpej 
    260  1.1   thorpej static const char *
    261  1.2       erh filetype_prologue(struct filetype *fit)
    262  1.2       erh {
    263  1.2       erh 	if (fit->fit_flags & FIT_NOPROLOGUE || *fit->fit_path == '/')
    264  1.2       erh 		return ("");
    265  1.2       erh 	else
    266  1.2       erh 		return ("$S/");
    267  1.2       erh }
    268  1.2       erh 
    269  1.2       erh static const char *
    270  1.1   thorpej prefix_prologue(const char *path)
    271  1.1   thorpej {
    272  1.1   thorpej 	if (*path == '/')
    273  1.1   thorpej 		return ("");
    274  1.1   thorpej 	else
    275  1.1   thorpej 		return ("$S/");
    276  1.1   thorpej }
    277  1.1   thorpej 
    278  1.5       dsl static void
    279  1.1   thorpej emitdefs(FILE *fp)
    280  1.1   thorpej {
    281  1.1   thorpej 	struct nvlist *nv;
    282  1.1   thorpej 	char *sp;
    283  1.1   thorpej 
    284  1.5       dsl 	fprintf(fp, "KERNEL_BUILD=%s\n", conffile);
    285  1.5       dsl 	fputs("IDENT=", fp);
    286  1.1   thorpej 	sp = "";
    287  1.1   thorpej 	for (nv = options; nv != NULL; nv = nv->nv_next) {
    288  1.1   thorpej 
    289  1.1   thorpej 		/* skip any options output to a header file */
    290  1.1   thorpej 		if (DEFINED_OPTION(nv->nv_name))
    291  1.1   thorpej 			continue;
    292  1.5       dsl 		fprintf(fp, "%s-D%s", sp, nv->nv_name);
    293  1.1   thorpej 		if (nv->nv_str)
    294  1.5       dsl 		    fprintf(fp, "=\"%s\"", nv->nv_str);
    295  1.1   thorpej 		sp = " ";
    296  1.1   thorpej 	}
    297  1.5       dsl 	putc('\n', fp);
    298  1.5       dsl 	fprintf(fp, "PARAM=-DMAXUSERS=%d\n", maxusers);
    299  1.5       dsl 	fprintf(fp, "MACHINE=%s\n", machine);
    300  1.1   thorpej 	if (*srcdir == '/' || *srcdir == '.') {
    301  1.5       dsl 		fprintf(fp, "S=\t%s\n", srcdir);
    302  1.1   thorpej 	} else {
    303  1.1   thorpej 		/*
    304  1.1   thorpej 		 * libkern and libcompat "Makefile.inc"s want relative S
    305  1.1   thorpej 		 * specification to begin with '.'.
    306  1.1   thorpej 		 */
    307  1.5       dsl 		fprintf(fp, "S=\t./%s\n", srcdir);
    308  1.1   thorpej 	}
    309  1.1   thorpej 	for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
    310  1.5       dsl 		fprintf(fp, "%s=%s\n", nv->nv_name, nv->nv_str);
    311  1.1   thorpej }
    312  1.1   thorpej 
    313  1.5       dsl static void
    314  1.1   thorpej emitobjs(FILE *fp)
    315  1.1   thorpej {
    316  1.1   thorpej 	struct files *fi;
    317  1.1   thorpej 	struct objects *oi;
    318  1.1   thorpej 	int lpos, len, sp;
    319  1.1   thorpej 
    320  1.5       dsl 	fputs("OBJS=", fp);
    321  1.1   thorpej 	sp = '\t';
    322  1.1   thorpej 	lpos = 7;
    323  1.1   thorpej 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    324  1.1   thorpej 		if ((fi->fi_flags & FI_SEL) == 0)
    325  1.1   thorpej 			continue;
    326  1.1   thorpej 		len = strlen(fi->fi_base) + 2;
    327  1.1   thorpej 		if (lpos + len > 72) {
    328  1.5       dsl 			fputs(" \\\n", fp);
    329  1.1   thorpej 			sp = '\t';
    330  1.1   thorpej 			lpos = 7;
    331  1.1   thorpej 		}
    332  1.5       dsl 		fprintf(fp, "%c%s.o", sp, fi->fi_base);
    333  1.1   thorpej 		lpos += len + 1;
    334  1.1   thorpej 		sp = ' ';
    335  1.1   thorpej 	}
    336  1.1   thorpej 	TAILQ_FOREACH(oi, &allobjects, oi_next) {
    337  1.1   thorpej 		if ((oi->oi_flags & OI_SEL) == 0)
    338  1.1   thorpej 			continue;
    339  1.1   thorpej 		len = strlen(oi->oi_path);
    340  1.2       erh 		if (*oi->oi_path != '/')
    341  1.2       erh 		{
    342  1.2       erh 			/* e.g. "$S/" */
    343  1.2       erh  			if (oi->oi_prefix != NULL)
    344  1.2       erh 				len += strlen(prefix_prologue(oi->oi_path)) +
    345  1.2       erh 				       strlen(oi->oi_prefix) + 1;
    346  1.2       erh 			else
    347  1.2       erh 				len += strlen(filetype_prologue(&oi->oi_fit));
    348  1.1   thorpej 		}
    349  1.1   thorpej 		if (lpos + len > 72) {
    350  1.5       dsl 			fputs(" \\\n", fp);
    351  1.1   thorpej 			sp = '\t';
    352  1.1   thorpej 			lpos = 7;
    353  1.1   thorpej 		}
    354  1.1   thorpej 		if (*oi->oi_path == '/') {
    355  1.5       dsl 			fprintf(fp, "%c%s", sp, oi->oi_path);
    356  1.1   thorpej 		} else {
    357  1.1   thorpej 			if (oi->oi_prefix != NULL) {
    358  1.5       dsl 				fprintf(fp, "%c%s%s/%s", sp,
    359  1.5       dsl 					    prefix_prologue(oi->oi_path),
    360  1.5       dsl 					    oi->oi_prefix, oi->oi_path);
    361  1.1   thorpej 			} else {
    362  1.5       dsl 				fprintf(fp, "%c%s%s", sp,
    363  1.2       erh 				            filetype_prologue(&oi->oi_fit),
    364  1.5       dsl 				            oi->oi_path);
    365  1.1   thorpej 			}
    366  1.1   thorpej 		}
    367  1.1   thorpej 		lpos += len + 1;
    368  1.1   thorpej 		sp = ' ';
    369  1.1   thorpej 	}
    370  1.5       dsl 	putc('\n', fp);
    371  1.1   thorpej }
    372  1.1   thorpej 
    373  1.5       dsl static void
    374  1.1   thorpej emitcfiles(FILE *fp)
    375  1.1   thorpej {
    376  1.1   thorpej 
    377  1.5       dsl 	emitfiles(fp, 'c', 0);
    378  1.1   thorpej }
    379  1.1   thorpej 
    380  1.5       dsl static void
    381  1.1   thorpej emitsfiles(FILE *fp)
    382  1.1   thorpej {
    383  1.1   thorpej 
    384  1.5       dsl 	emitfiles(fp, 's', 'S');
    385  1.1   thorpej }
    386  1.1   thorpej 
    387  1.5       dsl static void
    388  1.1   thorpej emitfiles(FILE *fp, int suffix, int upper_suffix)
    389  1.1   thorpej {
    390  1.1   thorpej 	struct files *fi;
    391  1.1   thorpej 	int lpos, len, sp;
    392  1.1   thorpej 	const char *fpath;
    393  1.3       erh  	struct config *cf;
    394  1.3       erh  	char swapname[100];
    395  1.1   thorpej 
    396  1.5       dsl 	fprintf(fp, "%cFILES=", toupper(suffix));
    397  1.1   thorpej 	sp = '\t';
    398  1.1   thorpej 	lpos = 7;
    399  1.1   thorpej 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    400  1.1   thorpej 		if ((fi->fi_flags & FI_SEL) == 0)
    401  1.1   thorpej 			continue;
    402  1.5       dsl 		fpath = srcpath(fi);
    403  1.1   thorpej 		len = strlen(fpath);
    404  1.5       dsl 		if (fpath[len - 1] != suffix && fpath[len - 1] != upper_suffix)
    405  1.1   thorpej 			continue;
    406  1.5       dsl 		if (*fpath != '/') {
    407  1.2       erh 			/* "$S/" */
    408  1.2       erh  			if (fi->fi_prefix != NULL)
    409  1.2       erh 				len += strlen(prefix_prologue(fi->fi_prefix)) +
    410  1.2       erh 				       strlen(fi->fi_prefix) + 1;
    411  1.2       erh 			else
    412  1.2       erh 				len += strlen(filetype_prologue(&fi->fi_fit));
    413  1.1   thorpej 		}
    414  1.1   thorpej 		if (lpos + len > 72) {
    415  1.5       dsl 			fputs(" \\\n", fp);
    416  1.1   thorpej 			sp = '\t';
    417  1.1   thorpej 			lpos = 7;
    418  1.1   thorpej 		}
    419  1.1   thorpej 		if (*fi->fi_path == '/') {
    420  1.5       dsl 			fprintf(fp, "%c%s", sp, fpath);
    421  1.1   thorpej 		} else {
    422  1.1   thorpej 			if (fi->fi_prefix != NULL) {
    423  1.5       dsl 				fprintf(fp, "%c%s%s/%s", sp,
    424  1.5       dsl 					    prefix_prologue(fi->fi_prefix),
    425  1.5       dsl 					    fi->fi_prefix, fpath);
    426  1.1   thorpej 			} else {
    427  1.5       dsl 				fprintf(fp, "%c%s%s", sp,
    428  1.2       erh 				            filetype_prologue(&fi->fi_fit),
    429  1.5       dsl 				            fpath);
    430  1.1   thorpej 			}
    431  1.1   thorpej 		}
    432  1.1   thorpej 		lpos += len + 1;
    433  1.1   thorpej 		sp = ' ';
    434  1.1   thorpej 	}
    435  1.3       erh  	/*
    436  1.3       erh  	 * The allfiles list does not include the configuration-specific
    437  1.3       erh  	 * C source files.  These files should be eliminated someday, but
    438  1.3       erh  	 * for now, we have to add them to ${CFILES} (and only ${CFILES}).
    439  1.3       erh  	 */
    440  1.3       erh  	if (suffix == 'c') {
    441  1.3       erh  		TAILQ_FOREACH(cf, &allcf, cf_next) {
    442  1.3       erh  			(void)snprintf(swapname, sizeof(swapname), "swap%s.c",
    443  1.3       erh  			    cf->cf_name);
    444  1.3       erh  			len = strlen(swapname);
    445  1.3       erh  			if (lpos + len > 72) {
    446  1.5       dsl  				fputs(" \\\n", fp);
    447  1.3       erh  				sp = '\t';
    448  1.3       erh  				lpos = 7;
    449  1.3       erh  			}
    450  1.5       dsl  			fprintf(fp, "%c%s", sp, swapname);
    451  1.3       erh  			lpos += len + 1;
    452  1.3       erh  			sp = ' ';
    453  1.3       erh  		}
    454  1.3       erh  	}
    455  1.5       dsl 	putc('\n', fp);
    456  1.1   thorpej }
    457  1.1   thorpej 
    458  1.1   thorpej /*
    459  1.1   thorpej  * Emit the make-rules.
    460  1.1   thorpej  */
    461  1.5       dsl static void
    462  1.1   thorpej emitrules(FILE *fp)
    463  1.1   thorpej {
    464  1.1   thorpej 	struct files *fi;
    465  1.1   thorpej 	const char *cp, *fpath;
    466  1.1   thorpej 	int ch;
    467  1.1   thorpej 
    468  1.1   thorpej 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    469  1.1   thorpej 		if ((fi->fi_flags & FI_SEL) == 0)
    470  1.1   thorpej 			continue;
    471  1.5       dsl 		fpath = srcpath(fi);
    472  1.1   thorpej 		if (*fpath == '/') {
    473  1.5       dsl 			fprintf(fp, "%s.o: %s\n", fi->fi_base, fpath);
    474  1.1   thorpej 		} else {
    475  1.1   thorpej 			if (fi->fi_prefix != NULL) {
    476  1.5       dsl 				fprintf(fp, "%s.o: %s%s/%s\n", fi->fi_base,
    477  1.5       dsl 					    prefix_prologue(fi->fi_prefix),
    478  1.5       dsl 					    fi->fi_prefix, fpath);
    479  1.1   thorpej 			} else {
    480  1.5       dsl 				fprintf(fp, "%s.o: %s%s\n",
    481  1.2       erh 				            fi->fi_base,
    482  1.2       erh 				            filetype_prologue(&fi->fi_fit),
    483  1.5       dsl 				            fpath);
    484  1.7      kent  			}
    485  1.7      kent 		}
    486  1.7      kent 		if (fi->fi_mkrule != NULL) {
    487  1.7      kent 			fprintf(fp, "\t%s\n\n", fi->fi_mkrule);
    488  1.7      kent 		} else {
    489  1.7      kent 			fputs("\t${NORMAL_", fp);
    490  1.7      kent 			cp = strrchr(fpath, '.');
    491  1.7      kent 			cp = cp == NULL ? fpath : cp + 1;
    492  1.7      kent 			while ((ch = *cp++) != '\0') {
    493  1.7      kent 				fputc(toupper(ch), fp);
    494  1.1   thorpej 			}
    495  1.7      kent 			fputs("}\n\n", fp);
    496  1.1   thorpej 		}
    497  1.1   thorpej 	}
    498  1.1   thorpej }
    499  1.1   thorpej 
    500  1.1   thorpej /*
    501  1.1   thorpej  * Emit the load commands.
    502  1.1   thorpej  *
    503  1.1   thorpej  * This function is not to be called `spurt'.
    504  1.1   thorpej  */
    505  1.5       dsl static void
    506  1.1   thorpej emitload(FILE *fp)
    507  1.1   thorpej {
    508  1.1   thorpej 	struct config *cf;
    509  1.1   thorpej 	const char *nm, *swname;
    510  1.1   thorpej 
    511  1.5       dsl 	fputs(".MAIN: all\nall:", fp);
    512  1.1   thorpej 	TAILQ_FOREACH(cf, &allcf, cf_next) {
    513  1.5       dsl 		fprintf(fp, " %s", cf->cf_name);
    514  1.1   thorpej 	}
    515  1.5       dsl 	fputs("\n\n", fp);
    516  1.1   thorpej 	TAILQ_FOREACH(cf, &allcf, cf_next) {
    517  1.1   thorpej 		nm = cf->cf_name;
    518  1.1   thorpej 		swname =
    519  1.1   thorpej 		    cf->cf_root != NULL ? cf->cf_name : "generic";
    520  1.5       dsl 		fprintf(fp, "KERNELS+=%s\n", nm);
    521  1.5       dsl 		fprintf(fp, "%s: ${SYSTEM_DEP} swap${.TARGET}.o vers.o", nm);
    522  1.5       dsl 		fprintf(fp, "\n"
    523  1.5       dsl 			    "\t${SYSTEM_LD_HEAD}\n"
    524  1.5       dsl 			    "\t${SYSTEM_LD} swap${.TARGET}.o\n"
    525  1.5       dsl 			    "\t${SYSTEM_LD_TAIL}\n"
    526  1.5       dsl 			    "\n"
    527  1.5       dsl 			    "swap%s.o: swap%s.c\n"
    528  1.5       dsl 			    "\t${NORMAL_C}\n\n", swname, swname);
    529  1.1   thorpej 	}
    530  1.1   thorpej }
    531  1.1   thorpej 
    532  1.1   thorpej /*
    533  1.1   thorpej  * Emit include headers (for any prefixes encountered)
    534  1.1   thorpej  */
    535  1.5       dsl static void
    536  1.1   thorpej emitincludes(FILE *fp)
    537  1.1   thorpej {
    538  1.1   thorpej 	struct prefix *pf;
    539  1.1   thorpej 
    540  1.1   thorpej 	SLIST_FOREACH(pf, &allprefixes, pf_next) {
    541  1.5       dsl 		fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
    542  1.5       dsl 		    prefix_prologue(pf->pf_prefix), pf->pf_prefix);
    543  1.1   thorpej 	}
    544  1.1   thorpej }
    545  1.1   thorpej 
    546  1.1   thorpej static int
    547  1.1   thorpej print_condmkopts(const char *name, void *value, void *arg)
    548  1.1   thorpej {
    549  1.1   thorpej 	struct nvlist *nv;
    550  1.1   thorpej 	FILE *fp = arg;
    551  1.1   thorpej 
    552  1.1   thorpej 	if (ht_lookup(selecttab, name) == 0)
    553  1.1   thorpej 		return (0);
    554  1.1   thorpej 
    555  1.1   thorpej 	for (nv = value; nv != NULL; nv = nv->nv_next)
    556  1.5       dsl 		fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
    557  1.1   thorpej 
    558  1.1   thorpej 	return (0);
    559  1.1   thorpej }
    560  1.1   thorpej 
    561  1.1   thorpej /*
    562  1.1   thorpej  * Emit appending makeoptions.
    563  1.1   thorpej  */
    564  1.5       dsl static void
    565  1.1   thorpej emitappmkoptions(FILE *fp)
    566  1.1   thorpej {
    567  1.1   thorpej 	struct nvlist *nv;
    568  1.1   thorpej 
    569  1.1   thorpej 	for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
    570  1.5       dsl 		fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
    571  1.1   thorpej 
    572  1.5       dsl 	ht_enumerate(condmkopttab, print_condmkopts, fp);
    573  1.1   thorpej }
    574